{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "详细配置参考：\n",
    "- https://wiki.archlinux.org/index.php/Hardware_video_acceleration_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)#VA-API\n",
    "- https://wiki.archlinux.org/index.php/Hardware_video_acceleration#VA-API_drivers\n",
    "\n",
    "- https://blogs.igalia.com/vjaquez/2018/03/28/gstreamer-va-api-troubleshooting/\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 查看当前显卡信息\n",
    "\n",
    "查看当前使用的显卡驱动\n",
    "\n",
    "```sh\n",
    "lspci -vnn | grep VGA -A 12\n",
    "lshw -C display\n",
    "sudo lshw -c video | grep configuration\n",
    "\n",
    "lspci |grep VGA \n",
    "\n",
    "```\n",
    "信息： \n",
    "```\n",
    "01:00.0 VGA compatible controller: NVIDIA Corporation GK106 [GeForce GTX 645 OEM] (rev a1)\n",
    "```\n",
    "\n",
    "解析：  \n",
    "GK106，是一个由英伟达公司制造的适配器图形处理和加速核心芯片。\n",
    "\n",
    "从NVIDIA支持的矩阵表中查找出GK106支持格式分别为：   \n",
    "NVDEC解码：MPEG-2 VC-1 H.264(AVCHD)   \n",
    "NVENC编码：H.264 (AVCHD) YUV 4:2:0\n",
    "\n",
    "- [NVIDIA支持的矩阵表](https://developer.nvidia.com/video-encode-decode-gpu-support-matrix)\n",
    "- [Linux中如何查看显卡硬件信息](https://www.linuxidc.com/Linux/2015-12/126627.htm)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Nvidia gstreamer硬件加速\n",
    "\n",
    "## 安装基础包\n",
    "\n",
    "```sh\n",
    "sudo apt-get install autoconf automake libtool build-essential  libass-dev pkg-config texinfo zlib1g-dev cmake mercurial libvorbis-dev libogg-dev git git-core libperl-dev libpciaccess-dev libpciaccess0 libfreetype6-dev libsdl2-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev wget zlib1g-dev    \n",
    "```\n",
    "\n",
    "## 安装VA-API（libav)相关库\n",
    "\n",
    "```sh\n",
    "sudo apt-get install libva libva-dev libva-x11-1 libx11-dev  libdrm-dev libva-glx1 libudev-dev mesa-common-dev xorg-dev vainfo\n",
    "```\n",
    "\n",
    "```sh\n",
    "sudo apt-get install libva libva-dev libx11-dev  libdrm-dev libudev-dev mesa-common-dev xorg-dev vainfo\n",
    "```\n",
    "\n",
    "\n",
    "\n",
    "## 安装VDPAU\n",
    "\n",
    "```sh\n",
    "sudo apt-get install libvdpau-dev libvdpau1 vdpauinfo\n",
    "```\n",
    "\n",
    "## 安装VDPAU后端驱动\n",
    "\n",
    "1. 方式一：直接安装\n",
    "\n",
    "```sh\n",
    "sudo apt install vdpau-va-driver\n",
    "```\n",
    "\n",
    "2. 方式二：编译安装最新版\n",
    "\n",
    "  驱动自带的`vdpau-va-driver`包有些bug\n",
    "`vdpau_EndPicture`: 在vdpau_endpicture函数中产生异常\n",
    "\n",
    "```sh\n",
    "git clone git://anongit.freedesktop.org/vaapi/vdpau-driver\n",
    "cd vdpau-driver/\n",
    "./autogen.sh --prefix=/usr\n",
    "make\n",
    "```\n",
    "  移除引起安装问题的链接：\n",
    "  \n",
    "```sh\n",
    "cd /usr/lib/arm-linux-gnueabihf/dri\n",
    "rm -f nvidia_drv_video.so s3g_drv_video.so\n",
    "cd -\n",
    "```\n",
    "\n",
    "  安装驱动器：\n",
    "```sh\n",
    "make install\n",
    "```\n",
    "\n",
    "## 确定VA-API系统正常工作\n",
    "\n",
    "详细配置参考：[Configuring_VDPAU](https://wiki.archlinux.org/index.php/Hardware_video_acceleration#Configuring_VDPAU)\n",
    "\n",
    "#在~/.bashrc文件中添加下面三行(永久)\n",
    "\n",
    "```sh\n",
    "export VDPAU_DRIVER=nvidia\n",
    "export LIBVA_DRIVER_NAME=vdpau\n",
    "export LIBVA_DRIVERS_PATH=/usr/lib/x86_64-linux-gnu/dri\n",
    "```\n",
    "查看系统信息：\n",
    "```sh\n",
    "vdpauinfo\n",
    "vainfo\n",
    "```\n",
    "\n",
    "正常情况下应该会有类似这样的输出：\n",
    "```sh\n",
    "libva: VA-API version 0.32.0\n",
    "libva: User requested driver 'vdpau'\n",
    "libva: Trying to open /usr/lib/arm-linux-gnueabihf/dri/vdpau_drv_video.so\n",
    "libva: va_openDriver() returns 0\n",
    "[VDPAU SUNXI] VE version 0x1623 opened.\n",
    "vainfo: VA-API version: 0.32 (libva 1.0.15)\n",
    "vainfo: Driver version: Splitted-Desktop Systems VDPAU backend for VA-API - 0.7.5.pre1\n",
    "vainfo: Supported profile and entrypoints\n",
    "      VAProfileMPEG2Simple            : VAEntrypointVLD\n",
    "      VAProfileMPEG2Main              : VAEntrypointVLD\n",
    "      VAProfileMPEG4Simple            : VAEntrypointVLD\n",
    "      VAProfileMPEG4AdvancedSimple    : VAEntrypointVLD\n",
    "      VAProfileH264Baseline           : VAEntrypointVLD\n",
    "      VAProfileH264Main               : VAEntrypointVLD\n",
    "      VAProfileH264High               : VAEntrypointVLD\n",
    "```      \n",
    "\n",
    "`VAEntrypointVLD` 意味着你可以解码这个格式  \n",
    "`VAEntrypointEncSlice`意味着你可以编码这个格式\n",
    "参考：\n",
    "\n",
    "- [香蕉派获取硬件加速](http://forum.lemaker.org/cn/thread-2237-1-1-%E3%80%90%E8%BD%AC%E3%80%91%E5%A6%82%E4%BD%95%E5%88%A9%E7%94%A8gstreamer%E6%88%90%E5%8A%9F%E8%8E%B7%E5%BE%97%E7%A1%AC%E4%BB%B6%E5%8A%A0%E9%80%9F%E8%A7%86%E9%A2%91.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Intel 硬件加速\n",
    "\n",
    "## 安装基础包\n",
    "\n",
    "```sh\n",
    "sudo apt-get install autoconf automake libtool build-essential  libass-dev pkg-config texinfo zlib1g-dev cmake mercurial libvorbis-dev libogg-dev git git-core libperl-dev libpciaccess-dev libpciaccess0 libfreetype6-dev libsdl2-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev wget zlib1g-dev    \n",
    "```\n",
    "\n",
    "## 安装VAAPI（libav)相关库\n",
    "\n",
    "```sh\n",
    "sudo apt-get install libdrm-intel1 libva1 libva-intel-vaapi-driver libva-x11-1 libx11-dev libva-dev libdrm-dev libudev-dev mesa-common-dev xorg-dev vainfo\n",
    "```\n",
    " vainfo\n",
    "源码安装Libav：\n",
    "\n",
    "```sh\n",
    "git clone https://github.com/intel/libva --depth=1\n",
    "cd libva\n",
    "./autogen.sh\n",
    "./configure\n",
    "make\n",
    "sudo make install\n",
    "```\n",
    "\n",
    "## 安装VAAPI后端驱动\n",
    "\n",
    "安装Intel集显驱动：\n",
    "\n",
    "方式一：在线源\n",
    "```sh\n",
    "sudo apt-get install i965-va-driver\n",
    "```\n",
    "方式二：编译安装\n",
    "\n",
    "**intel-vaapi-driver：**\n",
    "\n",
    "```sh\n",
    "git clone https://github.com/intel/intel-vaapi-driver --depth=1\n",
    "cd intel-vaapi-driver\n",
    "./autogen.sh\n",
    "./configure\n",
    "make\n",
    "sudo make install\n",
    "```\n",
    "\n",
    "## 检查\n",
    "\n",
    "首先查看是否有intel驱动：\n",
    "```sh\n",
    "apt list i965-va-driver\n",
    "#运行之后，有驱动说明成功安装\n",
    "```\n",
    "\n",
    "#在~/.bashrc文件中添加下面两行(永久)\n",
    "\n",
    "```sh\n",
    "export LIBVA_DRIVER_NAME=i965 \n",
    "export LIBVA_DRIVERS_PATH=/usr/lib/x86_64-linux-gnu/dri\n",
    "```\n",
    "\n",
    "查看libva版本\n",
    "```sh \n",
    "dpkg-query --showformat='${Package}: ${Version}\\n' --show | grep libva\n",
    "```\n",
    "查看vainfo版本\n",
    "```sh\n",
    "dpkg-query --showformat='${Package}: ${Version}\\n' --show | grep vainfo\n",
    "```\n",
    "查看i965版本\n",
    "```sh\n",
    "dpkg-query --showformat='${Package}: ${Version}\\n' --show | grep i965\n",
    "```\n",
    "\n",
    "主要是验证：\n",
    "```sh\n",
    "vainfo\n",
    "```\n",
    "\n",
    "其他安装完成之后，若还是运行vainfo没有显示\n",
    "```\n",
    "vainfo: Supported profile and entrypoints\n",
    "      VAProfileMPEG2Simple            :    VAEntrypointVLD\n",
    "      VAProfileMPEG2Simple            :    VAEntrypointEncSlice\n",
    "      VAProfileMPEG2Main              :    VAEntrypointVLD\n",
    "      VAProfileMPEG2Main              :    VAEntrypointEncSlice\n",
    "      VAProfileH264ConstrainedBaseline:    VAEntrypointVLD\n",
    "      VAProfileH264ConstrainedBaseline:    VAEntrypointEncSlice\n",
    "      VAProfileH264ConstrainedBaseline:    VAEntrypointEncSliceLP\n",
    "      VAProfileH264Main               :    VAEntrypointVLD\n",
    "      VAProfileH264Main               :    VAEntrypointEncSlice\n",
    "      VAProfileH264Main               :    VAEntrypointEncSliceLP\n",
    "      VAProfileH264High               :    VAEntrypointVLD\n",
    "```\n",
    "则需要以下操作：\n",
    "```\n",
    "vainfo --display drm --device /dev/dri/renderD128\n",
    "或者\n",
    "vainfo --display drm --device /dev/dri/renderD129\n",
    "```\n",
    "\n",
    "分别运行看那行代码有显示上面的内容。\n",
    "\n",
    "最后需要操作：\n",
    "\n",
    "```sh\n",
    "$ gedit ~/.bash_aliases\n",
    " \n",
    "alias vainfo='vainfo --display drm --device /dev/dri/renderD128'（或者129，看那个能用）\n",
    "#这个时候就可以直接使用vainfo\n",
    "```\n",
    "\n",
    "      \n",
    "## 使用gstreamer\n",
    "\n",
    "### 安装gstreamer包\n",
    "\n",
    "```sh\n",
    "apt-get install libgstreamer1.0-0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-doc gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio\n",
    "```\n",
    "\n",
    "### 安装gstreamer VAAPI的依赖包\n",
    "\n",
    "```sh\n",
    "apt-get install libglib2.0-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev mesa-vdpau-drivers\n",
    "```\n",
    "\n",
    "### 安装gstreamer VAAPI 插件\n",
    "\n",
    "- 方式一：\n",
    "\n",
    "```sh\n",
    "sudo apt install gstreamer1.0-vaapi\n",
    "```\n",
    "\n",
    "- 方式二：编译安装\n",
    "\n",
    "下载地址: [gstreamer-vaapi/](https://gstreamer.freedesktop.org/src/gstreamer-vaapi/)\n",
    "\n",
    "**注意**： 下载解压后检查 README 文件，里面写明了的依赖包以及版本环境，如果不支持当前环境，请重新下载新的或者旧的版本。（也可从configure脚本文件里查找 Gstreamer API定位到相关代码查看gstreamer 的版本要求。）\n",
    "\n",
    "解压 xz 文件：\n",
    "\n",
    "```sh\n",
    "xz -d gstreamer-vaapi-xxx.tar.xz\n",
    "```\n",
    "生成 tar 文件，再次解压：\n",
    "\n",
    "```sh\n",
    "tar -xvf gstreamer-vaapi-xxx.tar\n",
    "```\n",
    "编译安装：\n",
    "\n",
    "```sh\n",
    "cd gstreamer-vaapi/\n",
    "./autogen.sh --prefix=/usr\n",
    "make\n",
    "make install\n",
    "cd ..\n",
    "```\n",
    "\n",
    "### 检查是否成功\n",
    "\n",
    "```sh\n",
    "gst-inspect-1.0 | grep -i vaapi\n",
    "```\n",
    "正常情况下应该会有类似这样的输出：\n",
    "```\n",
    "vaapiparse:  vaapiparse_h264: H.264 parser\n",
    "vaapi:  vaapidecode: VA-API decoder\n",
    "vaapi:  vaapipostproc: VA-API video postprocessing\n",
    "vaapi:  vaapisink: VA-API sink\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AMD 硬件加速\n",
    "\n",
    "## 安装基础包\n",
    "\n",
    "## 安装XVBA后端驱动\n",
    "\n",
    "```sh\n",
    "sudo apt-get install xvba-va-driver\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 说明\n",
    "\n",
    "- **libva**：也就是传说中的 VAAPI，是一个开放源代码的提供硬件解码功能的模块；  \n",
    "- **i965-va-driver**：Intel 显卡的 VAAPI 后端，负责连接 Intel 显示卡驱动与 VAAPI。  \n",
    "- **xvba-video**：AMD/ATi 显卡的 VAAPI 后端，负责连接 AMD/ATi 显示卡驱动与 VAAPI。  \n",
    "- **vdpau**：NV 显卡的 VAAPI 后端, 负责连接 NV 显示卡驱动与 VAAPI。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 参考\n",
    "\n",
    "- [Intel VAAPI 详细安装](https://01.org/linuxmedia/documentation/source-code)\n",
    "\n",
    "- [gstreamer安装AV-VPI成功后没有查询不到vaapi插件问题解决](https://blog.csdn.net/DrBlack666/article/details/102669611)\n",
    "\n",
    "- [install mplayer with va-api hardware acceleration for intel amd gpus support in ubuntu](https://www.tuicool.com/articles/EFbU3m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "279.273px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
